/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;

import Models.KhuVuc_Model;
import Models.Ban_Model;
import Models.NhatKyHT_Model;
import Beans.NhanVien;

/**
 *
 * @author VIC
 */
public class KhuVuc_Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            if (action.equals("Add")) {   // Tạo khu vực
                String tenkv = request.getParameter("areaname");
                if (KhuVuc_Model.KiemTraTenKV(tenkv)) {
                    out.print("areaexist"); // khu vực đã tồn tại
                } else {
                    int makv = KhuVuc_Model.TaoMa();
                    KhuVuc_Model.LapKhuVuc(makv, tenkv);
                    NhanVien nv = new NhanVien();
                    nv = (NhanVien) session.getAttribute("nv");
                    NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "area");
                    KhuVuc_Model kvm = new KhuVuc_Model();
                    Vector vKhuvuc = new Vector();
                    vKhuvuc = kvm.LayKV(makv);
                    request.setAttribute("infoKV", vKhuvuc);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/arealang.jsp");
                    rd.forward(request, response);
                }
            } else if (action.equals("Edit")) {
                String tenkv = request.getParameter("tenkv");
                String makv = request.getParameter("makv");
                String mann = request.getParameter("mann");
                if (KhuVuc_Model.KiemTraSuaTenKV(tenkv, makv)) {
                    out.print("areaexist");
                } else {
                    KhuVuc_Model.Edit(makv, mann, tenkv);
                    NhanVien nv = new NhanVien();
                    nv = (NhanVien) session.getAttribute("nv");
                    NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "areaname");
                    KhuVuc_Model kvm = new KhuVuc_Model();
                    Vector vKhuvuc = new Vector();
                    vKhuvuc = kvm.LayKV(Integer.parseInt(makv));
                    request.setAttribute("infoKV", vKhuvuc);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/arealang.jsp");
                    rd.forward(request, response);
                }
            } else if (action.equals("Show")) { // Hiển thị trang quản lý khu vực
                String item = request.getParameter("item"); // trang hiện tại - phân trang
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector vKhuvuc = new Vector();
                vKhuvuc = kvm.LayDSKVTao(langid); // Lấy danh sách khu vực
                request.setAttribute("DSKV", vKhuvuc);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/newarea.jsp");
                rd.forward(request, response);
            } else if (action.equals("ShowKV")) {
                String item = request.getParameter("item");
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector v = new Vector();
                v = kvm.LayDSKVTao(langid);
                request.setAttribute("DSKV", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listarea.jsp");
                rd.forward(request, response);
            } else if (action.equals("ShowKVTao")) {
                String item = request.getParameter("item");
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector v = new Vector();
                v = kvm.LayDSKVTao(langid);
                request.setAttribute("DSKV", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listarea.jsp");
                rd.forward(request, response);
            } else if (action.equals("UpdateStatusKV")) {
                String makv = request.getParameter("makv");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                KhuVuc_Model.UpdateStatusKV(makv, status);
                Ban_Model.UpdateStatusBanFollowArea(makv, status);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "statusarea");
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector v = new Vector();
                v = kvm.LayDSKVTao(langid);
                request.setAttribute("item", item);
                request.setAttribute("DSKV", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listarea.jsp");
                rd.forward(request, response);
            } else if (action.equals("ShowLangKV")) {
                String makv = request.getParameter("makv");
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector v = new Vector();
                v = kvm.LayKV(Integer.parseInt(makv));
                request.setAttribute("infoKV", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/arealang.jsp");
                rd.forward(request, response);
            } else if (action.equals("SearchKV")) {
                String tenkv = request.getParameter("tenkv");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector v = new Vector();
                v = kvm.SearchKV(tenkv, status, langid);
                request.setAttribute("status", status);
                request.setAttribute("tenkv", tenkv);
                request.setAttribute("item", item);
                request.setAttribute("DSKV", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearcharea.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortTenKV")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector v = new Vector();
                v = kvm.SortTenKV(langid, order);
                request.setAttribute("DSKV", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortarea.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortTTKV")) {   // Sort tình trạng khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector v = new Vector();
                v = kvm.SortTTKV(langid, order);
                request.setAttribute("DSKV", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortarea.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTenKV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String tenkv = request.getParameter("tenkv");
                String status = request.getParameter("status");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector v = new Vector();
                v = kvm.SortSearchTenKV(langid, order, tenkv, status);
                request.setAttribute("DSKV", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("tenkv", tenkv);
                request.setAttribute("status", status);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearcharea.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTTKV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String tenkv = request.getParameter("tenkv");
                String status = request.getParameter("status");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector v = new Vector();
                v = kvm.SortSearchTTKV(langid, order, tenkv, status);
                request.setAttribute("DSKV", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("tenkv", tenkv);
                request.setAttribute("status", status);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearcharea.jsp");
                rd.forward(request, response);
            } else if (action.equals("test")) {
                int count = KhuVuc_Model.Test();
                request.setAttribute("count", count);
                RequestDispatcher rd = request.getRequestDispatcher("admin/test.jsp");
                rd.forward(request, response);
            }

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
